package cn.bxshey.resourceSharing.service.impl;

import cn.bxshey.resourceSharing.dto.essay.EssayDto;
import cn.bxshey.resourceSharing.entity.Essay;
import cn.bxshey.resourceSharing.mapper.EssayMapper;
import cn.bxshey.resourceSharing.service.EssayService;
import cn.bxshey.resourceSharing.vo.essay.EssayPageVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Essay)表服务实现类
 *
 * @author makejava
 * @since 2023-12-16 17:54:45
 */
@Service
public class EssayServiceImpl extends ServiceImpl<EssayMapper, Essay> implements EssayService {
    @Resource
    EssayMapper essayMapper;

    /*分页查询文章信息
     *
     */
    @Override
    public Map<String, Object> paginatedQueries(Integer current, Integer size, EssayDto essayDto) {
        Map<String, Object> map = new HashMap<>();
        map.put("records", essayMapper.paginatedQueries((current - 1) * size, size, essayDto));
        Long count = essayMapper.count(essayDto);
        map.put("page", count % size == 0 ? count / size : (count / size) + 1); //总页数
        map.put("count", count); //总数
        return map;
    }


    /*
     *前7热点文章
     */
    @Override
    public List<Essay> getHotArticle(String[] kind) {
        return essayMapper.getHotArticle(kind);
    }



    /**
     * 搜索文章
     *
     * @param current 第几页 1是第一页
     * @param size 查询几个
     * @param title 文章标题
     * @return  文章、总数、多少页
     */
    @Override
    public EssayPageVO essaySearch(Integer current, Integer size, String title) {
        EssayPageVO page = new EssayPageVO();
        page.setRecords(essayMapper.search((current-1)*size, size, title));
        Long num = essayMapper.searchCount(title);
        page.setCount(num);
        page.setPage((int) (num % size == 0 ? (num / size) : (num / size) + 1));
        return page;
    }


}

